package server.content.abilities;

import server.Server;
import server.game.players.Client;

public class BananaTree {

	private Client c;
	private int stage = -1;
	private int TREE_INFO[][] = { { 5, 2073 }, { 4, 2074 }, { 3, 2075 },
			{ 2, 2076 }, { 1, 2077 }, { 0, 2078 } };
	private int objectId;
	private int x, y;
	private boolean hasBananas;
	private int respawn = -1;

	public BananaTree(final Client c, final int objectId, final int x,
			final int y) {
		this.c = c;
		this.objectId = objectId;
		setX(x);
		setY(y);
		switch (this.objectId) {
		case 2073:
			setStage(5);
			break;
		case 2074:
			setStage(4);
			break;
		case 2075:
			setStage(3);
			break;
		case 2076:
			setStage(2);
			break;
		case 2077:
			setStage(1);
			break;
		case 2078:
			setStage(0);
			break;
		}
	}

	public void takeBanana() {
		if (getStage() > 0) {
			setStage(getStage() - 1);
			c.getItems().addItem(1963, 1);
			c.sendMessage("You manage to pick a banana.");
			updateTree();
		} else if (getStage() == 0) {
			updateTree();
			setRespawn(15);
			stage = -1;
			setHasBananas(false);
		} else if (getStage() == -1) {
			c.sendMessage("This tree has no bananas!");
		}
	}

	public void updateTree() {
		Server.objectHandler
				.createAnObject(c, getNewObjectId(), getX(), getY());
	}

	public int getNewObjectId() {
		for (int i = 0; i < TREE_INFO.length; i++) {
			if (TREE_INFO[i][0] == getStage()) {
				return TREE_INFO[i][1];
			}
		}
		return 2073;
	}

	public void setRespawn(final int respawn) {
		this.respawn = respawn;
	}

	public int getRespawn() {
		return respawn;
	}

	public boolean hasBananas() {
		return hasBananas;
	}

	public void setHasBananas(final boolean hasBananas) {
		this.hasBananas = hasBananas;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public void setX(final int x) {
		this.x = x;
	}

	public void setY(final int y) {
		this.y = y;
	}

	public Client getClient() {
		return c;
	}

	public int getObjectId() {
		return objectId;
	}

	public int getStage() {
		return stage;
	}

	public void setStage(final int stage) {
		this.stage = stage;
	}

}
